﻿//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using CuoiHoi.Entities;
using CuoiHoi.DataAccess;
 
namespace CuoiHoi.Business
{
    public class BAlbum
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "Album_SelectAll", null);
            return all;
        }
        public static DataTable Album_SelectAllByID(int idTheLoai)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idTheLoai", idTheLoai);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "Album_SelectAllByID", pr);
            return all;
        }
        
        public static DataTable SelectAllAnh()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "Album_SelectAllAnh", null);
            return all;
        }
        
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "Album_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(EAlbum OAlbum)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenAlbum", OAlbum.TenAlbum);
            pr[1] = new SqlParameter(@"idTheLoai", OAlbum.idTheLoai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"Album_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(EAlbum OAlbum)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"idAlbum", OAlbum.idAlbum);
            pr[1] = new SqlParameter(@"TenAlbum", OAlbum.TenAlbum);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"Album_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int idAlbum)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idAlbum",idAlbum);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Album_Delete", pr);
        }
//-----------------------------------------------------------//
        public static DataTable SelectByID(int idAlbum)
        {
    
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idAlbum",idAlbum);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "Album_SelectByID", pr);

            return all;
        }
//-----------------------------------------------------------//
        private static EAlbum GetOneAlbum(IDataReader idr)
       {
            EAlbum OAlbum = new EAlbum();
                if (idr["idAlbum"] != DBNull.Value)
                    OAlbum.idAlbum = (int)idr["idAlbum"];
                if (idr["TenAlbum"] != DBNull.Value)
                    OAlbum.TenAlbum = (string)idr["TenAlbum"];
                if (idr["idTheLoai"] != DBNull.Value)
                    OAlbum.idTheLoai = (int)idr["idTheLoai"];
            return OAlbum;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int idAlbum)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"idAlbum",idAlbum);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"Album_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<EAlbum> ListAll()
        {
            List<EAlbum> list = new List<EAlbum>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "Album_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneAlbum(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<EAlbum> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<EAlbum> list = new List<EAlbum>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "Album_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneAlbum(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
        // viết thêm

        public static EAlbum Select1ByOrder(int Top)
        {
            EAlbum OAlbum = new EAlbum();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "Album_Select1ByOrder", pr);
            if (idr.Read())
                OAlbum = GetOneAlbum(idr);
            idr.Close();
            idr.Dispose();
            return OAlbum;
        }

        // viết thêm 13/8/14

        public static EAlbum selectTenAlbumById(int idAlbum)
        {
            EAlbum OAlbum = new EAlbum();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idAlbum", idAlbum);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "Album_selectTenAlbumById", pr);
            if (idr.Read())
                OAlbum = GetOneAlbum(idr);
            idr.Close();
            idr.Dispose();
            return OAlbum;
        }

       
    }
 
}